/**
 * Copyright (c)2014 快诺迅汇(长沙)网络技术股份有限公司。
 * 版权所有
 */
package com.lehe.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lehe.common.MessageEnumDto;
import com.lehe.common.exceptions.LHServiceException;
import com.lehe.entry.wx.message.ReqMenuMessage;
import com.lehe.entry.wx.message.RespBaseMessage;
import com.lehe.service.IWeChatExecuteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


/**
 * 类的功能描述<br>
 *
 * @author 王丹
 * @FileName WeChatFormController.java<br>
 * @Language Java<br>
 * @date 2016-07-22<br>
 */

@Controller
@RequestMapping("/weChatForm")
public class WeChatFormController extends Basecontroller {

    @Autowired
    @Qualifier("menuCreateExecuteService")
    protected IWeChatExecuteService menuCreateExecuteService;

    @Autowired
    @Qualifier("menuQueryExecuteService")
    protected IWeChatExecuteService menuQueryExecuteService;

    @RequestMapping(value = "", method = {RequestMethod.GET, RequestMethod.POST})
    public String index(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String serviceType = request.getParameter("serviceType");
        String reqContent = request.getParameter("reqContent");
        try {
            RespBaseMessage respBaseMessage = new RespBaseMessage(MessageEnumDto.E9999);
            if("menuCreate".equalsIgnoreCase(serviceType)){
                ReqMenuMessage menuMessage = this.json2Obj(reqContent, ReqMenuMessage.class);
                
                respBaseMessage = menuCreateExecuteService.execute(menuMessage);
            }
            makeResponse(response, "application/json", obj2Json(respBaseMessage));
        }catch (LHServiceException e){
            e.printStackTrace();
            makeResponse(response, "text/html", e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            makeResponse(response, "text/html", "fail");
        }
        return null;
    }

    @RequestMapping(value = "menuQuery", method = {RequestMethod.GET, RequestMethod.POST})
    public String menuQuery(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            RespBaseMessage respBaseMessage = menuQueryExecuteService.execute(null);
            makeResponse(response, "application/json", obj2Json(respBaseMessage));
        }catch (LHServiceException e){
            e.printStackTrace();
            makeResponse(response, "text/html", e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            makeResponse(response, "text/html", "fail");
        }
        return null;
    }
}
